﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RememberLib
{
    public class ConfigOp
    {
        public const string CONFIGNAME_GlobalFlow = "GlobalFlow";

        public static int SaveConfig(string name, string value)
        {
            RememberEntities database = new RememberEntities();

            Config config = null;
            var result = database.Config.Where(c => c.ConfigName == name);
            if (result != null && result.Count() > 0)
            {
                config = result.First();
            }

            if (config == null)
            {
                config = new Config { ConfigName = name, ConfigValue = value, ConfigValueType = value.GetType().ToString() };
                database.AddToConfig(config);
            }
            else
            {
                config.ConfigValue = value ;
            }

            int rc = database.SaveChanges();
            return rc;
        }

        public static string LoadConfig( string name )
        {
            RememberEntities database = new RememberEntities();

            Config config = null;
            var result = database.Config.Where(c => c.ConfigName == name);
            if (result != null && result.Count() > 0)
            {
                config = result.First();
            }

            string configvalue = null;
            if (config != null)
                configvalue = config.ConfigValue;

            return configvalue;
        }
    }
}
